package com.hyh.toyshopping.service.impl;


import com.hyh.toyshopping.commons.CurUser;
import com.hyh.toyshopping.exceptions.AccountLockedException;
import com.hyh.toyshopping.exceptions.PasswordException;
import com.hyh.toyshopping.exceptions.UsernameNotFoundException;
import com.hyh.toyshopping.mapper.UserMapper;
import com.hyh.toyshopping.model.User;
import com.hyh.toyshopping.model.param.LoginParam;
import com.hyh.toyshopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @BelongsProject:IntelliJ IDEA
 * @BelongsPackage:com.hyh.myshopping.service.Impl
 * @Author:hyhWTX
 * @ClassName:UserServiceImpl
 * @CreateTime:2022年-12月-06日 19:38
 * @Description: User服务层实现
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /***
     * @Description: 用户登录
     * @param loginParam
     * @return CurUser
     */

    @Override
    public CurUser login(LoginParam loginParam) throws UsernameNotFoundException, PasswordException {
        //根据用户名获得用户信息
        User user = userMapper.getUserByName(loginParam.getUsername());
        //判断用户名是否存在
        if (user==null){
            throw new UsernameNotFoundException();
        }
        //判断用户状态
        if(user.getUser_status()==-1){
            throw new AccountLockedException();
        }
        //判断密码是否正确
        if (!user.getUser_password().equals(loginParam.getPassword())){
            //使用异常处理密码错误
            throw  new PasswordException();
        }

        CurUser curUser = new CurUser();
        curUser.setUser_id(user.getUser_id());
        curUser.setUser_name(user.getUser_name());
        curUser.setUser_sex(user.getUser_sex());
        user = null;
        return curUser;

    }

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public List<User> getUserList() {
        return userMapper.getUserList();
    }

    @Override
    public void updateStatus(int user_id, int user_status) {
        userMapper.updateStatus(user_id,user_status);
    }

    @Override
    public void changePasswordByUserName(String username,String userpassword) {
        userMapper.changePasswordByUserName(username,userpassword);

    }
}
